//
//  CQPopupInfo+Effect.m
//  AppCommonUICollect
//
//  Created by ciyouzen on 2019/8/13.
//  Copyright © 2019 dvlproad. All rights reserved.
//
//  修饰弹窗

#import "CQPopupInfo.h"
#import "CQEffectViewFactory.h"

#import "CQPopupBottomToolbarContentView.h"

@implementation CQPopupInfo (Effect)

/*
 *  显示当前视图到window底部(可自定制点击空白区域执行的是确认还是取消操作)
 *
 *  @param popupInfo                    popupInfo
 *  @param cornerType                   圆角类型
 *  @param effectType                   模糊类型
 *  @param selfBGType                   背景类型
 */
+ (void)configToolbarPopupInfo:(CQPopupInfo *)popupInfo
                withCornerType:(CQBottomPopupViewCornerRadiusFor)cornerType
                    effectType:(CQEffectFor)effectType
                    selfBGType:(CQBottomPopupViewBGTheme)selfBGType
{
    [CQPopupInfo configPopupInfo:popupInfo withCornerType:cornerType effectType:effectType];
    
    CQPopupBottomToolbarContentView *toolbarContentView = (CQPopupBottomToolbarContentView *)popupInfo.viewWithoutPanLine;
    toolbarContentView.toolbar.bgType = selfBGType;
    if (selfBGType == CQBottomPopupViewBGThemeBlack) {
        toolbarContentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
    } else if (selfBGType == CQBottomPopupViewBGThemeWhite) {
        toolbarContentView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
    } else {
        toolbarContentView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1.0];
    }
}

/*
 *  显示当前视图到window底部(可自定制点击空白区域执行的是确认还是取消操作)
 *
 *  @param popupInfo                    popupInfo
 *  @param cornerType                   圆角类型
 *  @param effectType                   模糊类型
 */
+ (void)configPopupInfo:(CQPopupInfo *)popupInfo
         withCornerType:(CQBottomPopupViewCornerRadiusFor)cornerType
             effectType:(CQEffectFor)effectType
{
    UIView *viewWithoutPanLine = popupInfo.viewWithoutPanLine;
    CGFloat viewHeightWithoutPanLine = CGRectGetHeight(popupInfo.popupViewShowFrame) - popupInfo.panLineAndSapcingHeight;
    
    UIVisualEffectView *effectView = nil;
    
    UIView *bBlankView = popupInfo.blankView;
    if (effectType == CQEffectForBlankViewWithDark) {
        [CQEffectViewFactory addEffectwithEffectType:UIBlurEffectStyleDark toView:bBlankView closeView:bBlankView];
    } else if (effectType == CQEffectForBlankViewWithLight) {
        [CQEffectViewFactory addEffectwithEffectType:UIBlurEffectStyleLight toView:bBlankView closeView:bBlankView];
        //bBlankView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
    } else if (effectType == CQEffectForBlankViewWithBGColor) {
        UIColor *blankBGColor = [UIColor colorWithRed:17/255.0 green:17/255.0 blue:17/255.0 alpha:.8];
        bBlankView.backgroundColor = blankBGColor;
    } else if (effectType == CQEffectForContentViewWithLight) {
        effectView = [CQEffectViewFactory addEffectwithEffectType:UIBlurEffectStyleLight toView:viewWithoutPanLine.superview closeView:viewWithoutPanLine];
    } else {
        bBlankView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.6];
    }

    
    if (cornerType == CQBottomPopupViewCornerRadiusForContentView) {
        if ([viewWithoutPanLine isKindOfClass:[UIScrollView class]]) {
            NSLog(@"警告⚠️⚠️⚠️⚠️⚠️：添加圆角失败。\
                  原因：此要添加圆角的视图是滚动列表，需要去取视图的contentSize的高度作为bezierPathWithRoundedRect，\
                  但是此时内容存在没添加上去的情况，导致无法准确获取contentSize的高度，所以请\
                  方法①自己在自己的滚动视图里实现，\
                  方法②将该视图包进一个UIView里，然后剩下交给我");
        } else {
            CGRect borderRect = CGRectMake(0, 0, CGRectGetWidth(popupInfo.popupViewShowFrame), viewHeightWithoutPanLine);
            NSAssert(CGRectGetWidth(borderRect) != 0 && CGRectGetHeight(borderRect) != 0, @"宽高都不能为0");
            UIBezierPath *fieldPath = [UIBezierPath bezierPathWithRoundedRect:borderRect byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(30 , 30)];
            
            CAShapeLayer *fieldLayer = [[CAShapeLayer alloc] init];
            fieldLayer.frame = borderRect;
            fieldLayer.path = fieldPath.CGPath;
            viewWithoutPanLine.layer.mask = fieldLayer;
            
            CAShapeLayer *fieldLayer2 = [[CAShapeLayer alloc] init];
            fieldLayer2.frame = borderRect;
            fieldLayer2.path = fieldPath.CGPath;
            effectView.layer.mask = fieldLayer2;
        }

    } else if (cornerType == CQBottomPopupViewCornerRadiusForRealPopupView1) {
        viewWithoutPanLine.layer.cornerRadius = 10;
        viewWithoutPanLine.layer.masksToBounds = YES;
    } else {

    }
}


/*
 *  显示当前视图到window中间
 *
 *  @param popupInfo                    popupInfo
 *  @param effectType                   模糊类型
 */
+ (void)configCenterPopupInfo:(CQPopupInfo *)popupInfo
                   effectType:(CQEffectFor)effectType
{
    UIView *bBlankView = popupInfo.blankView;
    if (effectType == CQEffectForBlankViewWithDark) {
        [CQEffectViewFactory addEffectwithEffectType:UIBlurEffectStyleDark toView:bBlankView closeView:bBlankView];
    } else if (effectType == CQEffectForBlankViewWithLight) {
        [CQEffectViewFactory addEffectwithEffectType:UIBlurEffectStyleLight toView:bBlankView closeView:bBlankView];
        //bBlankView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
    } else if (effectType == CQEffectForBlankViewWithBGColor) {
        UIColor *blankBGColor = [UIColor colorWithRed:17/255.0 green:17/255.0 blue:17/255.0 alpha:.8];
        bBlankView.backgroundColor = blankBGColor;
    }
}

@end
